home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / pd / utilities / tinymeter / source / tinymeter_main / main.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  8KB  |  432 lines

  1. #include <libraries/commodities.h>
  2. #include <proto/SysInfo.h>
  3. #include <libraries/SysInfo.h>
  4. #include <libraries/ScreenNotify.h>
  5. #include <exec/memory.h>
  6. #include <dos/dos.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <intuition/cghooks.h>
  11. #include <intuition/icclass.h>
  12. #include <intuition/classes.h>
  13. #include <utility/tagitem.h>
  14. #include "gaugeclass.h"
  15. #include "launchclass.h"
  16. #include "tinymeter.h"
  17. /* defaultshit */
  18. #define Version "4.30"
  19.  
  20. #ifndef MAKE_ID
  21. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  22. #endif                                                                       
  23.  
  24. static const char *version="$VER: TinyMeter V" Version;
  25.  
  26. struct Library  *RetinaBase;
  27. struct Library  *ShutdownBase;
  28. struct Library  *TimerBase;
  29. struct Library  *SysInfoBase;
  30. struct Library  *ScreenNotifyBase;
  31. struct Library  *CxBase;
  32. struct Library  *DataTypesBase;
  33. struct Library  *LocaleBase;
  34.  
  35. struct SysInfo  *si;
  36. ULONG  executive;
  37.  
  38. struct WBStartup *_WBBenchMsg;
  39. ULONG *TM_Pool; /* Global memory pool */
  40.  
  41. __geta4 ULONG pAllocVec(ULONG memSize)
  42. {
  43.     ULONG *memory; memSize += sizeof(ULONG) + 8L;
  44.     if(memory=(ULONG *)AllocPooled(TM_Pool,memSize))
  45.     {
  46.     memory[0]=memSize;
  47.     memory+=2L;
  48.     }
  49.     return(memory);
  50. }
  51. __geta4 void  pFreeVec (ULONG *memory)
  52. {
  53.     memory-=2L;
  54.     FreePooled(TM_Pool,memory,memory[0]);
  55. }
  56.  
  57. show(int what)
  58. {
  59.     static struct EasyStruct easymsg =
  60.     {
  61.     sizeof(struct EasyStruct),
  62.     0,
  63.     "TinyMeter " Version " information",
  64.     0L,
  65.     "OK"
  66.     };
  67.     static char *err_msg[]=
  68.     {
  69.     "Not enough memory.",
  70.     "Could not open the window.",
  71.     "Port initialization failed",
  72.     "BOOPSI allocation failed",
  73.     "timer.device error!",
  74.     "Startup problem!",
  75.     "Preferences error. Using defaults!",
  76.     "Could not open listfile"
  77.     };
  78.  
  79.     easymsg.es_TextFormat=err_msg[what];
  80.     EasyRequest(NULL, &easymsg, NULL);
  81. }
  82.  
  83. int getNum(struct tm_gau_set *list)
  84. {
  85.     int num=1;
  86.     while(list=list->next)num++;
  87.     return(num);
  88. }
  89.  
  90. useDefault(struct tm_sys_set *set,struct tm_data *data)
  91. {
  92.     struct tm_gau_set   *many,
  93.             *act,
  94.             *old;
  95.  
  96.     static struct tm_sys_set default_set=
  97.     {
  98.     "TM40",
  99.     1,
  100.  
  101.     10,
  102.     10,
  103.     450,
  104.     2,
  105.  
  106.     "Workbench",
  107.  
  108.     "",
  109.     8,
  110.  
  111.     bg_none,
  112.     "",
  113.     FALSE,
  114.     -3,
  115.     FALSE,0x55555555,0x55555555,0x55555555,
  116.  
  117.     5,
  118.     1,
  119.  
  120.     0,
  121.     TRUE,
  122.  
  123.     win_normal,
  124.     0,
  125.     4,
  126.     4,
  127.     8,
  128.     8,
  129.  
  130.     bd_standard,
  131.     FALSE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  132.     FALSE,0x0,0x0,0x0,
  133.  
  134.     idle_own
  135.     };
  136.  
  137.     static struct tm_gau_set g3=
  138.     {
  139.     typ_idle,
  140.     typ_histmeter,
  141.  
  142.     200,
  143.  
  144.     "",
  145.     8,
  146.  
  147.     "ChipMem",
  148.     "CPU %p%% free",
  149.     "",
  150.     ind_right,
  151.     TRUE,
  152.     TRUE,
  153.     TRUE,
  154.     TRUE,
  155.     FALSE,
  156.     FALSE,
  157.     TRUE,
  158.  
  159.     TRUE,2,0,0,
  160.     TRUE,2,0,0,
  161.     FALSE,0,0xFFFFFFFF,0,
  162.     FALSE,0,0,0xFFFFFFFF,
  163.     FALSE,0xFFFFFFFF,0,0,
  164.     TRUE,2,0,0,
  165.     TRUE,1,0,0,
  166.     TRUE,1,0,0,
  167.  
  168.     NULL
  169.     };
  170.  
  171.     static struct tm_gau_set g2=
  172.     {
  173.     typ_fast,
  174.     typ_gauge,
  175.  
  176.     20,
  177.  
  178.     "",
  179.     8,
  180.  
  181.     "FastMem",
  182.     "%td Bytes free",
  183.     "",
  184.     ind_right,
  185.     TRUE,
  186.     TRUE,
  187.     TRUE,
  188.     TRUE,
  189.     FALSE,
  190.     FALSE,
  191.     FALSE,
  192.  
  193.     TRUE,2,0,0,
  194.     TRUE,2,0,0,
  195.     FALSE,0,0xFFFFFFFF,0,
  196.     FALSE,0,0,0xFFFFFFFF,
  197.     FALSE,0xFFFFFFFF,0,0,
  198.     TRUE,2,0,0,
  199.     TRUE,1,0,0,
  200.     TRUE,0,0,0,
  201.  
  202.     &g3
  203.     };
  204.  
  205.  
  206.     static struct tm_gau_set g1=
  207.     {
  208.     typ_chip,
  209.     typ_gauge,
  210.  
  211.     20,
  212.  
  213.     "topaz.font",
  214.     8,
  215.  
  216.     "ChipMem",
  217.     "%td Bytes free",
  218.     "",
  219.     ind_right,
  220.     TRUE,
  221.     TRUE,
  222.     TRUE,
  223.     TRUE,
  224.     FALSE,
  225.     FALSE,
  226.     FALSE,
  227.  
  228.     TRUE,2,0,0,
  229.     TRUE,2,0,0,
  230.     FALSE,0,0xFFFFFFFF,0,
  231.     FALSE,0,0,0xFFFFFFFF,
  232.     FALSE,0xFFFFFFFF,0,0,
  233.     TRUE,2,0,0,
  234.     TRUE,1,0,0,
  235.     TRUE,0,0,0,
  236.  
  237.     &g2
  238.     };
  239.  
  240.     CopyMem((char *)&default_set,set,sizeof(struct tm_sys_set));
  241.  
  242.     if(act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set)))
  243.     {
  244.     many=&g1; data->list=act; CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  245.     while(many=many->next)
  246.     {
  247.         old=act;
  248.         if(act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set)))
  249.         {
  250.         old->next=act;
  251.         CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  252.         }
  253.         else {act->next=0L;return;}
  254.     }
  255.     act->next=0L;
  256.     }
  257.     else data->list=0L;
  258. }
  259.  
  260. struct NewBroker newbroker =
  261. {
  262.     NB_VERSION,
  263.     "TinyMeter V" Version,
  264.     "TinyMeter V" Version " © by Tinic Urou",
  265.     "Shows system states",
  266.     NBU_UNIQUE,
  267.     0,
  268.     -3,
  269.     0,
  270.     0
  271. };
  272.  
  273. int main(void)
  274. {
  275.  
  276.     struct tm_sys_set   *set;
  277.     struct tm_data      *data;
  278.  
  279.     Class               *gclass,*lclass;
  280.     ULONG                cxsigflag;
  281.     CxMsg               *msg;
  282.     CxObj               *broker;
  283.     struct MsgPort      *broker_mp;
  284.     ULONG               broker_err;
  285.  
  286.     if(LocaleBase=(struct Library *)OpenLibrary("locale.library",0L))
  287.     {
  288.     if(DataTypesBase=(struct Library *)OpenLibrary("datatypes.library",0L))
  289.     {        
  290.         if(CxBase=(struct Library *)OpenLibrary("commodities.library",0L))
  291.         {
  292.         if(TM_Pool=(ULONG *)CreatePool(MEMF_PUBLIC|MEMF_CLEAR,32768L,16384L))
  293.         {
  294.             if(data=(struct tm_data *)pAllocVec(sizeof(struct tm_data)))
  295.             {
  296.             if(set=(struct tm_sys_set *)pAllocVec(sizeof(struct tm_sys_set)+8L))
  297.             {                                   
  298.                 if((gclass=(Class *)initGaugeGadgetClass())&&(lclass=(Class *)initLauncherGadgetClass()))
  299.                 {
  300.                 char  *my_file;
  301.                 struct tm_gau_set *many,*act;
  302.                 if(my_file=(UBYTE *)Open("ENV:TinyMeter",MODE_OLDFILE))
  303.                 {
  304.                     if(!Read(my_file,set,(ULONG)sizeof(struct tm_sys_set)))
  305.                     {
  306.                     show(err_nopre);
  307.                     useDefault(set,data);
  308.                     }
  309.                     else
  310.                     {
  311.                     if((ULONG)*((ULONG *)&set->set_header[0])==0x544D3430)
  312.                     {
  313.                         if(act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set)))
  314.                         {
  315.                         data->list=act; many=act;
  316.                         while(Read((char *)my_file,(char *)act,(ULONG)sizeof(struct tm_gau_set))&&act)
  317.                         {
  318.                             many=act;
  319.                             if(act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set))) many->next=act;
  320.                             else DisplayBeep(0);
  321.                         }
  322.                         many->next=NULL; pFreeVec(act); act=NULL;
  323.                         }
  324.                         else DisplayBeep(0);
  325.                     }
  326.                     else
  327.                     {
  328.                         show(err_nopre);
  329.                         useDefault(set,data);
  330.                     }
  331.                     }
  332.                     Close(my_file);
  333.                 }
  334.                 else
  335.                 {
  336.                     show(err_nopre);
  337.                     useDefault(set,data);
  338.                 }
  339.                 data->num_of_gaug=getNum(data->list);
  340.                 SetTaskPri(FindTask(0L),set->pri);
  341.                 Delay(set->start_wait*50L);
  342.                 if(broker_mp = (struct MsgPort *)CreatePort(0,0))
  343.                 {
  344.                     newbroker.nb_Port = (struct MsgPort *)broker_mp;
  345.                     if (broker = (CxObj *)CxBroker(&newbroker, &broker_err))
  346.                     {
  347.                     cxsigflag = 1L << broker_mp->mp_SigBit;
  348.                     ActivateCxObj(broker, 1L);
  349.  
  350.                     RetinaBase=(struct Library *)OpenLibrary("retina.library",0L);
  351.  
  352.                     switch (set->Executive)
  353.                     {
  354.                         case    idle_none:
  355.                             executive=idle_none;
  356.                             break;
  357.                         case    idle_executive:
  358.                             if(SysInfoBase = (struct Library *)OpenLibrary(SYSINFONAME, SYSINFOVERSION))
  359.                             {
  360.                                 if(si =(struct SysInfo *)InitSysInfo())
  361.                                 executive=idle_executive;
  362.                                 else
  363.                                 executive=idle_none;
  364.                             }
  365.                             else executive=idle_none;
  366.                             break;
  367.                         case    idle_own:
  368.                             init_idle();
  369.                             executive=idle_own;
  370.                             break;
  371.                     }
  372.  
  373.                     data->scrnot=FALSE; if(set->start_usescreennotify)
  374.                         if(ScreenNotifyBase=(struct Library *)OpenLibrary("screennotify.library",SCREENNOTIFY_VERSION))
  375.                         data->scrnot=TRUE;
  376.  
  377.                     if(InitWorkbenchPath(data))
  378.                     {
  379.                         if(openWindow(set,data,gclass,lclass))
  380.                         {
  381.                         set=(struct tm_sys_set *)handler(set,data,broker_mp,broker,cxsigflag,gclass,lclass);
  382.                         closeWindow(set,data);
  383.                         }
  384.                         else show(err_nowin);
  385.                         FreeWorkbenchPath(data);
  386.                     }
  387.                     else show(err_nosta);
  388.                     switch (executive)
  389.                     {
  390.                         case    idle_executive:
  391.                             FreeSysInfo(si);
  392.                             CloseLibrary(SysInfoBase);
  393.                             break;
  394.                         case    idle_own:
  395.                             free_idle();
  396.                             break;
  397.  
  398.                     }
  399.                     if(data->scrnot)CloseLibrary(ScreenNotifyBase);
  400.                     if(RetinaBase)  CloseLibrary(RetinaBase);
  401.                     while(msg = (CxMsg *)GetMsg(broker_mp)) ReplyMsg((struct Message *)msg);
  402.                     DeleteCxObjAll(broker);
  403.                     }
  404.                     else show(err_nosta);
  405.                     DeletePort(broker_mp);
  406.                 }
  407.                 else show(err_nosta);
  408.                 freeGaugeGadgetClass(gclass);
  409.                 freeLauncherGadgetClass(lclass);
  410.                 }
  411.                 else show(err_noboo);
  412.             }
  413.             else show(err_nomem);
  414.             }
  415.             else show(err_nomem);
  416.             DeletePool(TM_Pool); /* also deletes our allocated mem */
  417.         }
  418.         CloseLibrary(CxBase);
  419.         }
  420.         CloseLibrary(DataTypesBase);
  421.     }
  422.     CloseLibrary(LocaleBase);
  423.     }
  424. }
  425.  
  426. int wbmain(void)
  427. {
  428.     return(main());
  429. }
  430.  
  431.  
  432.